<?php

namespace App\Admin\Controllers;

use App\Models\GoodsClassify;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class GoodsClassifyController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '商品分类';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new GoodsClassify());

        $grid->number('序号');
        $grid->rows(function ($row, $number) {
            $row->column('number', $number+1);
        });
        $grid->column('title', __('Title'));
        $grid->column('description', __('Description'));
        $grid->column('sort', __('Sort'));
        $grid->column('created_at', __('Created at'));

        // 筛选
        $grid->filter(function($filter){

            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->like('name', __('Name'));

            // 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
//            $filter->scope('trashed', '回收站')->onlyTrashed();
        });

        $grid->actions(function ($action) {
            $action->disableView();
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(GoodsClassify::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('title', __('Title'));
        $show->field('description', __('Description'));
        $show->field('sort', __('Sort'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
//        $show->field('deleted_at', __('Deleted at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new GoodsClassify());

        $form->text('title', __('Title'));
        $form->text('description', __('Description'));
        $form->number('sort', __('Sort'));
        $form->hidden('store_id');
        
        $form->submitted(function ($from) {
            $from->store_id = Admin::user()->store_id;
        });

        return $form;
    }
}
